Skip to content

refactor: DB 쿼리 최적화 및 방 입장 동시성 개선#21

Merged
Sean-mn merged 3 commits intodevelopfrom
refactor/db
Mar 12, 2026
Merged

refactor: DB 쿼리 최적화 및 방 입장 동시성 개선#21
Sean-mn merged 3 commits intodevelopfrom
refactor/db

Conversation

@Sean-mn
Copy link
Contributor

@Sean-mn Sean-mn commented Mar 12, 2026

📚작업 내용

  • CloseAsync: SELECT + SaveChanges 2번 왕복을 ExecuteUpdateAsync 1번으로 줄임
  • JoinRoomUseCase: 전체 엔티티 UPDATE 방식(UpdateAsync(Room))을 IncrementPlayerCountAsync로 교체해 current_players만 원자적으로 증가, 동시 입장 경쟁 조건 해결
  • SteamAuthTicketValidator: ReadAsStringAsync + Deserialize 조합을 ReadFromJsonAsync 스트림 역직렬화로 교체해 중간 문자열 할당 제거
  • AppDbContext: GetAllAsyncWHERE status ORDER BY created_at 쿼리를 위한 (status, created_at) 복합 인덱스 추가
  • Program.cs: EF Core 마이그레이션 히스토리 테이블을 room 스키마로 지정 (DB 유저 권한 이슈 대응)
  • 마이그레이션 파일 추가 (AddRoomStatusCreatedAtIndex), dev/prod DB 적용 완료

◀️참고 사항

IncrementPlayerCountAsync는 DB 레벨에서 current_players < max_players 조건을 포함한 조건부 UPDATE를 실행하며, 0건 업데이트 시 FULL_ROOM 예외를 반환합니다. 도메인의 room.Join() 호출은 선행 검증(빠른 실패)으로 유지됩니다.

✅체크리스트

[ ]안에 x를 작성하면 체크박스를 체크할 수 있습니다.

  • 현재 의도하고자 하는 기능이 정상적으로 작동하나요?
  • 변경한 기능이 다른 기능을 깨뜨리지 않나요?

추후 필요한 체크리스트는 업데이트 될 예정입니다.

Sean-mn and others added 2 commits March 12, 2026 15:11
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@gemini-code-assist
Copy link

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

이 PR은 데이터베이스 쿼리 최적화와 방 입장 로직의 동시성 문제를 해결하는 데 중점을 둡니다. EF Core의 ExecuteUpdateAsyncReadFromJsonAsync와 같은 효율적인 기능을 활용하여 데이터 접근 패턴을 개선하고, GetAllAsync 쿼리 성능 향상을 위해 새로운 복합 인덱스를 추가했습니다. 또한, 방 입장 시 플레이어 수 증가를 원자적으로 처리하여 동시성 문제를 방지하고, 필요한 데이터베이스 마이그레이션 및 설정 변경 사항을 포함합니다.

Highlights

  • DB 쿼리 최적화: CloseAsync 메서드가 SELECT + SaveChanges의 두 번의 DB 왕복 대신 ExecuteUpdateAsync를 사용하여 한 번의 작업으로 최적화되었습니다.
  • 방 입장 동시성 개선: JoinRoomUseCase에서 전체 엔티티를 업데이트하는 방식(UpdateAsync(Room))을 IncrementPlayerCountAsync로 교체하여 current_players만 원자적으로 증가시키고 동시 입장 경쟁 조건을 해결했습니다.
  • Steam 인증 티켓 유효성 검사 최적화: SteamAuthTicketValidator에서 ReadAsStringAsync + Deserialize 조합을 ReadFromJsonAsync 스트림 역직렬화로 교체하여 중간 문자열 할당을 제거했습니다.
  • 인덱스 추가: AppDbContextGetAllAsync 쿼리(WHERE status ORDER BY created_at)를 위한 (status, created_at) 복합 인덱스가 추가되었습니다.
  • EF Core 마이그레이션 스키마 설정: Program.cs에서 EF Core 마이그레이션 히스토리 테이블을 room 스키마로 지정하여 DB 사용자 권한 이슈에 대응했습니다.
  • 마이그레이션 적용: AddRoomStatusCreatedAtIndex 마이그레이션 파일이 추가되었고, 개발 및 운영 DB에 적용이 완료되었습니다.
Changelog
  • PushAndPull/Server/Application/Port/Output/Persistence/IRoomRepository.cs
    • UpdateAsync(Room room) 메서드가 제거되었습니다.
    • IncrementPlayerCountAsync(string roomCode) 메서드가 추가되었습니다.
  • PushAndPull/Server/Application/UseCase/Room/JoinRoomUseCase.cs
    • 방 입장 로직에서 _roomRepository.UpdateAsync(room) 호출이 _roomRepository.IncrementPlayerCountAsync(request.RoomCode)로 대체되었고, FULL_ROOM 예외 처리가 추가되었습니다.
  • PushAndPull/Server/Infrastructure/Auth/SteamAuthTicketValidator.cs
    • System.Net.Http.Json 네임스페이스가 추가되었습니다.
    • ParseResponseAsync 메서드에서 ReadAsStringAsyncJsonSerializer.Deserialize 대신 ReadFromJsonAsync를 사용하도록 변경되었습니다.
  • PushAndPull/Server/Infrastructure/Persistence/DbContext/AppDbContext.cs
    • Room 엔티티에 (Status, CreatedAt) 복합 인덱스(idx_room_status_created_at)가 추가되었습니다.
  • PushAndPull/Server/Infrastructure/Persistence/Repository/RoomRepository.cs
    • UpdateAsync(Room room) 메서드가 제거되었습니다.
    • IncrementPlayerCountAsync 메서드가 ExecuteUpdateAsync를 사용하여 CurrentPlayers를 원자적으로 증가시키도록 구현되었습니다.
    • CloseAsync 메서드가 ExecuteUpdateAsync를 사용하여 Room의 StatusExpiresAt를 직접 업데이트하도록 리팩토링되었습니다.
  • PushAndPull/Server/Migrations/20260312060119_AddRoomStatusCreatedAtIndex.Designer.cs
    • 새로운 EF Core 마이그레이션 디자이너 파일이 추가되었습니다.
  • PushAndPull/Server/Migrations/20260312060119_AddRoomStatusCreatedAtIndex.cs
    • Room 엔티티의 상태와 생성일자에 대한 복합 인덱스를 생성하는 새로운 EF Core 마이그레이션 파일이 추가되었습니다.
  • PushAndPull/Server/Migrations/AppDbContextModelSnapshot.cs
    • EF Core 데이터베이스 모델 스냅샷이 업데이트되었습니다.
  • PushAndPull/Server/Program.cs
    • Npgsql용 EF Core 마이그레이션 히스토리 테이블이 room 스키마를 사용하도록 구성되었습니다.
  • PushAndPull/Tests/UseCase/Room/JoinRoomUseCaseTests.cs
    • WhenAllConditionsAreValidForJoiningARoom 테스트가 UpdateAsync 대신 IncrementPlayerCountAsync 호출을 검증하도록 업데이트되었습니다.
Activity
  • 새로운 AddRoomStatusCreatedAtIndex 마이그레이션이 생성되었고, 개발 및 운영 데이터베이스에 성공적으로 적용되었습니다.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

전반적으로 훌륭한 리팩토링입니다. ExecuteUpdateAsync를 활용하여 DB 왕복을 줄이고, ReadFromJsonAsync로 불필요한 메모리 할당을 제거하는 등 성능 최적화가 인상적입니다. 특히 IncrementPlayerCountAsync를 통해 방 입장 시의 경쟁 조건을 해결한 점이 좋습니다.

한 가지 개선 제안 사항으로, JoinRoomUseCase에서 IncrementPlayerCountAsync가 실패했을 때의 오류 처리를 조금 더 정교하게 다듬는 것을 고려해볼 수 있습니다. 관련 내용은 코드에 직접 코멘트를 남겼습니다. 이 부분을 보완하면 더욱 견고한 코드가 될 것입니다.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@Sean-mn Sean-mn merged commit dc3064e into develop Mar 12, 2026
1 check passed
@Sean-mn Sean-mn deleted the refactor/db branch March 12, 2026 06:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant